package com.qfedu.job;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.common.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.apache.mahout.cf.taste.impl.similarity.EuclideanDistanceSimilarity;
import java.util.ArrayList;
import java.util.List;

//@Component
public  class ItemCFJob {
    final  int NEIGHBORHOOD_NUM = 5;   //用户邻居数量
    final  int RECOMMENDER_NUM = 5;    //推荐结果个数
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
//    @Scheduled(fixedDelay=5000)
    public void Itemcf() throws TasteException {
        try {
            //随机种子
            RandomUtils.useTestSeed();
            MysqlDataSource dataSource=new MysqlDataSource();
            dataSource.setUser("root");
            dataSource.setPassword("admin123");
            //dataSource.setDatabaseName("user_hobby");
            dataSource.setURL("jdbc:mysql://118.31.245.151:3306/me?useUnicode=true&characterEncoding=utf-8&useSSL=false");
            //dataSource.setURL("jdbc:mysql://localhost:3306/recommend?useUnicode=true&characterEncoding=utf-8&useSSL=false");
            DataModel model = new MySQLJDBCDataModel(dataSource, "user_hobby",
                    "user_id", "item_id", "score",
                    "CREATETIME");
            ItemSimilarity item = new EuclideanDistanceSimilarity(model);
            Recommender r = new GenericItemBasedRecommender(model,item);
            System.out.println();
            //得到所有用户的id集合
            LongPrimitiveIterator iter = model.getUserIDs();
            while(iter.hasNext()) {
                Long uid = iter.nextLong();
                List<RecommendedItem> list = r.recommend(uid,RECOMMENDER_NUM);  //获取推荐结果
                ObjectMapper om = new ObjectMapper();
                System.out.printf("item:uid:%s",uid);
                //遍历推荐结果
                ArrayList<Integer> items = new ArrayList<>();
                for(RecommendedItem ritem : list) {
                    System.out.printf("(%s,%f)",ritem.getItemID(),ritem.getValue());
                    items.add((int) ritem.getItemID());
                }
                String s = om.writeValueAsString(items);
                stringRedisTemplate.boundHashOps("ItemCF").put(uid.toString(),s);
                System.out.println();
            }
        } catch (TasteException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}